function[ncid, X_ID, Y_ID, Z_ID] = Create_Geomag_netCDF(localdirectory, lat, long, alt, metadatacellarray)

% Creates a netCDF file for storing Geomagnetic Data

% Fills in Global and Variable Attributes







nc_fname = [upper(cell2mat(metadatacellarray(1))) '_1995_2012_Secular_Removed.nc'];



S1 = dir([localdirectory nc_fname]);



if isempty(S1),

    

    ncid = netcdf.create([localdirectory nc_fname],'NC_NOCLOBBER');

    

    % Create Global Attributes

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'title', 'Geomagnetic time series data');

    

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'Summary', ...
        'Geomagnetic time series data from 1996 to 2010. The data set contains X,Y and Z components of the geomagnetic field measured  at one station.');

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'keywords', 'Geomagnetism, WDC, INTERMAGNET, 1 minute, Observatory name');

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'id', nc_fname);

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'history', 'The data were originally collected by the Institutions operating magnetic observatories. The data were then archived by the world data centers and the INTERMAGNET.');

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'date_created', datestr(now));

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'creator_name', ' ');

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'creator_email', ' ');

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'creator_url', ' ');

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'institution', ' ' );

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'project', ' ' );

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'),...
        'processing_level', 'Absolute data are checked for baseline variations. The erroneous sections were removed, but not corrected. The magnetic secular variations were removed by fitting the all the measured observatory data with cubic splines with knots separated by 1 year in a least-square sense. ' );

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'acknowledgment', 'Respective Institutions operating the magnetic observatories.' );

    

    

    station_name = cell2mat (metadatacellarray(2) );

    if isempty(station_name),

        station_name = 'Not Available';

    end;

    

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'station_name', station_name);

    

    station_name = cell2mat (metadatacellarray(6) );

    if isempty(station_name),

        station_name = 'Not Available';

    end;

    

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'station_country', station_name );

    

    station_name = cell2mat (metadatacellarray(8) );

    if isempty(station_name),

        station_name = 'Not Available';

    end;

    

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'station_institution', station_name );

    

    station_name = cell2mat (metadatacellarray(7) );

    if isempty(station_name),

        station_name = 'Not Available';

    end;

    

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'station_institution_url',station_name );

    

    station_name = cell2mat (metadatacellarray(9) );

    if isempty(station_name),

        station_name = 'Not Available';

    end;

    

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'station_institution_email', station_name );

    

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'geospatial_lat', single(lat));

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'geospatial_lon', single(long));

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'geospatial_alt', single(alt) );

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'time_coverage_start_unix_sec_since_1970_01_01', ...
        int32((datenum(1995,1,1,0,0,0)-datenum(1970,1,1))* 24 * 3600) );

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'time_coverage_end_unix_sec_since_1970_01_01',...
        int32((datenum(2012,12,31,23,59,0)-datenum(1970,1,1))* 24 * 3600));

    netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'), 'time_coverage_resolution_sec',...
        int32(60) );

    

    data_time_axis_dim = netcdf.defDim(ncid,'data_time_axis_dim',...
    length((datenum(1995,1,1,0,0,30): (1/(24*60)): datenum(2012,12,31,23,59,30))));   
% sum(daysinyear(1995:2012))*24*60);
   

    

    

    X_ID = netcdf.defVar(ncid,'Magnetic_Field_X','int', data_time_axis_dim);

    Y_ID = netcdf.defVar(ncid,'Magnetic_Field_Y','int', data_time_axis_dim);

    Z_ID = netcdf.defVar(ncid,'Magnetic_Field_Z','int', data_time_axis_dim);

    

    netcdf.putAtt(ncid,X_ID,'_FillValue',int32(999999));

    netcdf.putAtt(ncid,X_ID,'Unit','nT * 10');

    netcdf.putAtt(ncid,Y_ID,'_FillValue',int32(999999));

    netcdf.putAtt(ncid,Y_ID,'Unit','nT * 10');

    netcdf.putAtt(ncid,Z_ID,'_FillValue',int32(999999));

    netcdf.putAtt(ncid,Z_ID,'Unit','nT * 10');

    

    netcdf.endDef(ncid);

    

else

    

    ncid = netcdf.open([localdirectory nc_fname],'NC_WRITE');

    X_ID = netcdf.inqVarID(ncid,'Magnetic_Field_X');

    Y_ID = netcdf.inqVarID(ncid,'Magnetic_Field_Y');

    Z_ID = netcdf.inqVarID(ncid,'Magnetic_Field_Z');

    

    

    

end;









